<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\v1\giftcard;

use app\controller\admin\AuthController;
use app\model\v1\gift\GiftCard;
use app\model\v1\gift\GiftCardCate;
use app\model\v1\recovery\SystemOperationLog;
use app\model\v1\setup\SetupLimits;
use app\model\v1\setup\SetupRole;
use app\model\v1\setup\SystemAdmin;
use app\Request;
use crmeb\services\CacheService;
use think\facade\App;

/**
 * 设置管理
 * Class ArticleCategory
 * @package app\controller\admin\v1\cms
 */
class SetUpCollection extends AuthController
{
    protected $model;

    public function __construct(App $app, GiftCardCate $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }


    /**
     * 设置后台 - 菜单管理列表
     * @param Request $request
     * @return mixed
     */
    public function limitsList(Request $request)
    {
        $param = $request->param();
        $param['limit'] = $param["limit"] ?? 10;
        $param['page'] = $param["page"] ?? 1;

        $data = SetupLimits::getSetUpLimitsList($param);

        return $this->success($data);
    }


    /**
     * 菜单管理列表 - 编辑
     * @param Request $request
     * @return mixed
     */
    public function edit_limits(Request $request)
    {
        $param = $request->param();
        unset($param['token']);
        if (empty($param['id'])) return $this->fail('菜单id不能为空');

        $res = SetupLimits::where("id", $param["id"])->update($param);

        if($res >= 0) return $this->success('保存成功');
        return $this->fail('保存失败');
    }


    /**
     * 菜单管理列表 - 添加
     * @param Request $request
     * @return mixed
     */
    public function add_limits(Request $request)
    {
        $request_url = $request->baseUrl();
        $request_url = str_replace('/index.php/','',$request_url);

        $param = $request->param();
        $pid = isset($param['pid']) ? $param['pid'] : 'empty';
        if($pid == 'empty') return $this->fail('上级菜单不能为空');

        if(empty($param['name'])) return $this->fail('name菜单名称不能为空');
        if(empty($param['url'])) return $this->fail('url跳转地址不能为空');
        //if(empty($param['url_name'])) return $this->fail('url_name路由名称不能为空');
        if(!empty($param["url"])) {
            $data = SetupLimits::where("url", $param['url'])->where("is_del", 0)->find();
            if($data) return $this->fail('重复添加');
        }

        $param["pid"] = $pid;
        $res = SetupLimits::add_limits($param);
        if($res > 0){
            return $this->success('success');
        }
        return $this->fail('新增失败');
    }


    /**
     * 设置后台 - 账号管理列表
     * @param Request $request
     * @return void
     */
    public function admin_user_list(Request $request)
    {
        $param = $request->param();
        $param['page'] = $param['page'] ?? 1;
        $param['limit'] = $param['limit'] ?? 10;

        $data = SystemAdmin::admin_user_list($param);

        return $this->success($data);
    }


    /**
     * 新增账号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function admin_user_add(Request $request)
    {
        $param = $request->param();
        unset($param['token']);

        if(empty($param['account'])) return $this->fail('account不能为空');
        if(empty($param['real_name'])) return $this->fail('real_name不能为空');
        if(empty($param['pwd'])) return $this->fail('pwd不能为空');
        if(empty($param['phone'])) return $this->fail('phone不能为空');
        if(empty($param['role_id'])) return $this->fail('role_id不能为空');

        try {
            $res = SystemAdmin::admin_user_add($param);
            if($res > 0) return $this->success('新增成功');
            return $this->fail('新增失败');
        }catch (\Throwable $e){
            return $this->fail($e->getMessage());
        }
    }



    /**
     * 账号编辑
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function admin_user_up(Request $request)
    {
        $param = $request->param();
        unset($param['token']);

        if(empty($param['id'])) return $this->fail('id不能为空');
        if(empty($param['account'])) return $this->fail('account不能为空');

        try {
            $res = SystemAdmin::admin_user_up($param);
            if($res >= 0) return $this->success('编辑成功');
            return $this->fail('编辑失败');
        }catch (\Throwable $e){
            return $this->fail($e->getMessage());
        }
    }


    /**
     * 账号删除
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function admin_user_del(Request $request)
    {
        $param = $request->param();
        if(empty($param['id'])) return $this->fail('id不能为空');

        $admin = SystemAdmin::where('id',$param['id'])->find();
        if ($admin['account'] == 'admin') return $this->fail('超级管理员不可删除');

        $res = SystemAdmin::where('id',$param['id'])->update(["is_del" => 1]);

        return $this->success('删除成功');
    }


    /**
     * 获取所有角色列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoleList(Request $request)
    {
        $param = $request->param();

        $data = SetupRole::where("is_del", 0)
            ->where(function ($query) use ($param){
                if(!empty($param["seller_id"])) $query->where("seller_id", $param["seller_id"]);
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order('id','desc')
            ->select()
            ->toArray();

        return $this->success($data);
    }


    /**
     * 角色管理列表
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function role_list(Request $request)
    {
        $param = $request->param();
        $param['page'] = $param['page'] ?? 1;
        $param['limit'] = $param['limit'] ?? 10;

        $data = SetupRole::role_list($param);

        return $this->success($data);
    }


    /**
     * 新增角色
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function role_add(Request $request)
    {
        $param = $request->param();
        if (empty($param['name'])) return $this->fail('name不能为空');

        try {
            $res = SetupRole::role_add($param);
            if($res > 0) return $this->success('新增成功');
            return $this->fail('新增失败');
        }catch (\Throwable $e){
            return $this->fail($e->getMessage());
        }
    }


    /**
     * 角色删除
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function role_del(Request $request)
    {
        $param = $request->param();
        if (empty($param['id'])) return $this->fail('id不能为空');

        $res = SetupRole::where('id',$param['id'])->update(["is_del" => 1]);

        return $this->success('success');
    }


    /**
     * 角色赋予权限
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function role_up(Request $request)
    {
        $param = $request->param();
        unset($param['token']);
        if(empty($param['id'])) return $this->fail('id不能为空');
        if(empty($param['child_ids'])) return $this->fail('child_ids不能为空');
        if(!empty($param['parent_ids'])){
            $param["limit_id"] = $param['parent_ids'] ."=". $param['child_ids'];
            $param["limits_id"] = $param['parent_ids'] . ",".$param['child_ids'];
        }else{
            $param["limit_id"] = $param['child_ids'];
            $param["limits_id"] = $param['child_ids'];
        }

        try {
            $res = SetupRole::role_up($param);
            if($res >= 0) return $this->success('编辑成功');
            return $this->fail('编辑失败');
        }catch (\Throwable $e){
            return $this->fail($e->getMessage());
        }
    }


    //所有权限列表
    public function limits_()
    {
        $data = $this->getSonsInfo('0');
        return $this->success($data);
    }


    public function getSonsInfo($pid=0)
    {
        $lists = [];
        $items = SetupLimits::where('pid',$pid)->where("is_del", 0)->select()->toArray();

        foreach ($items as $item){
            $item['son'] = $this->getSonsInfo($item['id']);
            $lists[] = $item;
        }
        return $lists;
    }










    /**
     * 礼品卡管理列表 - 添加
     * @param Request $request
     * @return mixed
     */
    public function addGiftCard(Request $request)
    {
        $param = $request->post();
        if(empty($param['tag_id'])) return $this->fail('tag_id不能为空');
        if(empty($param['title'])) return $this->fail('title不能为空');
        if(empty($param['price'])) return $this->fail('price不能为空');
        if(empty($param['img'])) return $this->fail('img不能为空');
        if(empty($param['detail_chart'])) return $this->fail('detail_chart不能为空');
        if(empty($param['theme'])) return $this->fail('theme不能为空');
        if(empty($param['content'])) return $this->fail('content不能为空');
        if(empty($param['unit'])) return $this->fail('unit不能为空');
        if(empty($param['stock_num'])) return $this->fail('stock_num不能为空');
        if(empty($param['original_price'])) return $this->fail('original_price不能为空');
        if(empty($param['subscript_price'])) return $this->fail('subscript_price不能为空');
        if(empty($param['make_start_price'])) return $this->fail('make_start_price不能为空');
        if(!empty($param["bless"])){
            //if(empty($param['ground'])) return $this->fail('ground不能为空');
        }

        $res = GiftCard::addGiftCard($param);
        if($res > 0){
            return $this->success('success');
        }
        return $this->fail('添加失败');
    }


    /**
     * 礼品卡管理列表 - 编辑
     * @param Request $request
     * @return mixed
     */
    public function updateGiftCard(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');
        if(empty($param['title'])) return $this->fail('title不能为空');

        $res = GiftCard::updateGiftCard($param);

        $admin_id = $this->adminId;
        $ip = $request->ip();
        $desc = "-将id为{$param['id']}的数据编辑为：标题：{$param['title']}.";
        SystemOperationLog::addSystemLog($admin_id,'giftcard','礼品卡管理列表','updateGiftCard','编辑',$param,$ip,$desc);

        if($res >= 0){
            return $this->success('编辑成功');
        }
        return $this->fail('编辑失败');
    }



    /**
     * 礼品卡管理列表 - 删除
     * @return void
     */
    public function deleteGiftCard(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');

        $res = GiftCard::where("id", $param['id'])->update(['delete_time'=>time()]);

        $admin_id = $this->adminId;
        $ip = $request->ip();
        $desc = "-将id为{$param['id']}的数据删除.";
        SystemOperationLog::addSystemLog($admin_id,'giftcard','礼品卡管理列表','deleteGiftCard','删除',$param,$ip,$desc);

        if ($res >= 0)  return $this->success('删除成功');
        return $this->fail('删除失败');
    }


    /**
     * 礼品卡管理列表 - 启用禁用
     * @return void
     */
    public function enableGiftCard(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');

        $msg = GiftCard::enableGiftCard($param);

        return $this->success($msg);
    }





}
